module gui.logindlg;

import mirdefine, mirshare, mirgame, mirsound;
import gui.gwindow, gui.gbutton;
import common.imagearray;

import AGui.AGui;
import AGui.Widgets.TextField.TextField;

import share.utility;

import allegro5.allegro;
import allegro5.allegro_image;



const POS_ID_INS_X = 193;
const POS_ID_INS_Y = 435;
const POS_PASS_INS_X = 390;
const POS_PASS_INS_Y = 435;

const _LEFT_LOGIN = 0;
const _TOP_LOGIN = 372;
const _RIGHT_LOGIN = 640;
const _BOTTOM_LOGIN = 480;

const _LEFT_ID_PASS = 170;
const _TOP_ID_PASS = 65;

const _IMG_IDX_LOGIN_BORDER = 1;
const _IMG_IDX_LOGIN_IDPASS = 2;
const _IMG_IDX_BTN_LOGIN = 10;
const _IMG_IDX_BTN_NEW = 12;
const _IMG_IDX_BTN_CHANGE = 14;
const _IMG_IDX_BTN_EXIT = 16;


const _LEFT_LOGIN_BTN = 140;
const _LEFT_NEW_BTN = 205;
const _LEFT_CHANGE_BTN = 325;
const _LEFT_EXIT_BTN = 475;
const _TOP_LOGIN_BTNS = 8;

public class LoginWindow : GWindow, ActionListener
{
    private TextField DEdId;
    private TextField DEdPassword;
    private GButton DLoginOk;
    private GButton DLoginNew;
    private GButton DLoginChgPw;
    private GButton DLoginClose;
   

    public void actionPerformed(ActionEvent evt)
    {
        GButton button = cast(GButton)evt.getSource();
        if (button !is null)
        {
            clickSound(evt);
            
            if (button is DLoginOk)
            {
                string userid, passwd;
                userid = cast(string)DEdId.getText();
                passwd = cast(string)DEdPassword.getText();
                MySocket.SendLogin(userid, passwd);
            }
            
            if (button is DLoginClose)
            {
                LoginWindow.hide();
                //TODO: close application..
            }
        }
    }
	
    public this()
	{
        DEdId = new TextField();
        DEdId.setSize(100, 16);
        DEdId.setLocation(10, 10);
        DEdId.setMaxLength(20);
        
        DEdPassword = new TextField();
        DEdPassword.setSize(100, 16);
        DEdPassword.setLocation(120, 10);
        DEdPassword.setIsPassword(true);
        DEdPassword.setMaxLength(20);

        DLoginOk = new GButton();
        DLoginOk.addActionListener(this);

        DLoginNew = new GButton();
        DLoginNew.addActionListener(this);

        DLoginChgPw = new GButton();
        DLoginChgPw.addActionListener(this);
        
        DLoginClose = new GButton();
        DLoginClose.addActionListener(this);


        this.add(DEdId);
        this.add(DEdPassword);
        this.add(DLoginOk);
        this.add(DLoginNew);
        this.add(DLoginChgPw);
        this.add(DLoginClose);
        
        guiMan.add(this);
	}

    public ~this()
    {
        guiMan.remove(this);
        
        delete DEdId;
        delete DEdPassword;
        delete DLoginOk;
        delete DLoginNew;
        delete DLoginChgPw;
        delete DLoginClose;
    }
    
    public void createLoginWindow(ImageArray iarr)
    {
        super.createWindow(iarr, _IMG_IDX_LOGIN_BORDER, false, false, 0, 0);
        //super.setSize(640, 150);
        super.setLocation(_LEFT_LOGIN, _TOP_LOGIN);
        
        DLoginOk.createButton(iarr, _IMG_IDX_BTN_LOGIN, _IMG_IDX_BTN_LOGIN+1, _LEFT_LOGIN_BTN, _TOP_LOGIN_BTNS, false);
        DLoginOk.setVisibility(true);

        DLoginNew.createButton(iarr, _IMG_IDX_BTN_NEW, _IMG_IDX_BTN_NEW+1, _LEFT_NEW_BTN, _TOP_LOGIN_BTNS, false);
        DLoginNew.setVisibility(true);

        DLoginChgPw.createButton(iarr, _IMG_IDX_BTN_CHANGE, _IMG_IDX_BTN_CHANGE+1, _LEFT_CHANGE_BTN, _TOP_LOGIN_BTNS, false);
        DLoginChgPw.setVisibility(true);
        
        DLoginClose.createButton(iarr, _IMG_IDX_BTN_EXIT, _IMG_IDX_BTN_EXIT+1, _LEFT_EXIT_BTN, _TOP_LOGIN_BTNS, false);
        DLoginClose.setVisibility(true);
    }
	
	public void reset()
	{
		/*DEdId.visible = true;
		DEdPassword.visible = true;
		
		DEdId.setFocus(true);*/
	}
	
    private void clickSound(ActionEvent evt)
	{
		SoundMan.PlaySound(s_norm_button_click);
	}
}

